﻿using glb;
using PIMS.DBModel;
using PIMS.Interface;
using PIMS.WorkPlugin.ORM;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace PIMS.Print
{
    public partial class Form箱标补打 : Form
    {
        private IDebugMsg debug = null;
        private ILog4net log = null;

        PrintDocument PrinterBox;
        //Font FontTitle;
        //Font FontText;
        //Font FontTail;
        //Bitmap qrBmp;
        string lableConfigFile = AppDomain.CurrentDomain.BaseDirectory + "箱标签配置.xml";
        LabelXmlPrintHelper xmlPrint = null;

        string regkeypath = @"SOFTWARE\YongXiangProject\Form箱标补打";
        DataTable searchResult = null;

        public Form箱标补打()
        {
            InitializeComponent();
            this.comboBox1.Items.Clear();

            PrinterBox = new PrintDocument();
            PrinterBox.PrintController = new StandardPrintController(); //取消打印弹窗
            //FontTitle = new Font("微软雅黑", 14, FontStyle.Bold);
            //FontText = new Font("微软雅黑", 10);
            //FontTail = new Font("微软雅黑", 6);

            //绑定打印事件
            PrinterBox.PrintPage += Printer_PrintPageBox;
            xmlPrint = new LabelXmlPrintHelper(lableConfigFile);
        }

        /// <summary>
        /// 注：反射加载时会自动调用一次
        /// </summary>
        public void Init(IDebugMsg debugMsg, ILog4net log4net)
        {
            this.debug = debugMsg;
            this.log = log4net;
        }

        private void btn搜索箱标_Click(object sender, EventArgs e)
        {
            //if (String.IsNullOrEmpty(this.comboBox1.Text))
            //{
            //    MessageBox.Show("请输入检索条件！","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
            //    return;
            //}
            this.btn搜索箱标.Enabled = false;
            string water= "%"+tbWater.Text;
            string info = tbHead.Text + tbLine.Text + tbDate.Value.ToString("yyMMdd") + water;
            try
            {
                GetBoxInfo(info);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "发生错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.btn搜索箱标.Enabled = true;
        }
        private void Form箱标补打_Load(object sender, EventArgs e)
        {
            this.labelHelp.Text = "使用说明：检索条件分3部分，产线、日期、流水.\r\n" +
                "产线号A线是3，B线是4；日期自动取当前日期；流水号可输箱标后几位也可保持为0.\r\n" +
                "检索完，从箱标下拉列表中选择要补打的箱标。";

            List<string> list = GetLocalPrinter();
            this.箱标打印机.Items.Clear();
            foreach (string str in list)
            {
                this.箱标打印机.Items.Add(str);
            }
            this.箱标打印机.Text = RegistryTool.ReadReg(regkeypath, "箱标打印机");

            this.tbLine.SelectedIndex = 0;
            tbDate.Value = DateTime.Now;
        }

        void Printer_PrintPageBox(object sender, PrintPageEventArgs e)
        {
            //横向打印
            e.PageSettings.Landscape = true;

            string weight = "";
            if (!String.IsNullOrEmpty(this.重量.Text))
            {
                weight = this.重量.Text + "KG";
            }

            Dictionary<string, string> myDict = new Dictionary<string, string>();
            myDict.Add("型号", this.型号.Text);
            myDict.Add("批号", this.批号.Text);
            myDict.Add("重量", weight);
            myDict.Add("粒径", this.粒径.Text);
            myDict.Add("箱标", xmlPrint.barcode);
            xmlPrint.CreateLabelByXml(e.Graphics, myDict);

            if(this.checkBox1.Checked)
            {
                this.Close();
            }
        }
        /// <summary>
        ///  获取本地打印机的列表，第一项就是默认打印机
        /// </summary>
        public static List<string> GetLocalPrinter()
        {
            List<string> fPrinters = new List<string>();
            foreach (string fPrinterName in PrinterSettings.InstalledPrinters)
            {
                if (!fPrinters.Contains(fPrinterName))
                    fPrinters.Add(fPrinterName);
            }
            return fPrinters;
        }

        private void btn打印箱标_Click(object sender, EventArgs e)
        {
            if (0 > 箱标打印机.SelectedIndex)
            {
                MessageBox.Show("请选择打印机！！！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (String.IsNullOrEmpty(箱标.Text) || String.IsNullOrEmpty(型号.Text))
            {
                MessageBox.Show("箱标或型号为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            btn打印箱标.Enabled = false;
            string barcode = 箱标.Text;
            //qrBmp = 标签打印程序.ZxingQRCode.GenerateQRCode(barcode);
            xmlPrint.barcode = barcode;

            //指定打印机，非默认
            PrinterBox.PrinterSettings.PrinterName = this.箱标打印机.Text; //"Microsoft XPS Document Writer";
            //加入注释
            PrinterBox.PrinterSettings.PrintToFile = true;
            PrinterBox.Print();
            btn打印箱标.Enabled = true;

        }

        private void GetBoxInfo(string boxLabel)
        {
            //ArrayList sqlList = new ArrayList();
            string cmdSql = string.Concat(new string[]
               {
                  "select * from tb_saveddata where uid like '",
                  boxLabel,
                  "%'"
               });

            this.箱标.Text = ""; 
            this.批号.Text = "";
            this.型号.Text = "";
            this.粒径.Text = "";
            this.重量.Text = "";
            this.comboBox1.Items.Clear();

            //searchResult = DbHelperSQL.Queryy(cmdSql);
            searchResult = SqlSugarHelper.QueryToDataTable<saveddata>(cmdSql);
            if (searchResult != null && searchResult.Rows.Count > 0)
            {
                //this.箱标.Text = searchResult.Rows[0]["箱标"].ToString();
                //this.批号.Text = searchResult.Rows[0]["批号"].ToString();
                //this.型号.Text = searchResult.Rows[0]["型号"].ToString();
                //this.粒径.Text = searchResult.Rows[0]["粒径"].ToString();
                //this.重量.Text = searchResult.Rows[0]["重量"].ToString();
                for (int i = 0; i < searchResult.Rows.Count;i++ )
                {
                    this.comboBox1.Items.Add(searchResult.Rows[i]["uid"].ToString());
                }

                this.comboBox1.SelectedIndex = 0;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(this.comboBox1.Text))
            {
                this.箱标.Text = "";
                this.批号.Text = "";
                this.型号.Text = "";
                this.粒径.Text = "";
                this.重量.Text = "";
            }
            else
            {
                for (int i = 0; i < searchResult.Rows.Count; i++)
                {
                    if (this.comboBox1.Text == searchResult.Rows[i]["uid"].ToString())
                    {
                        this.箱标.Text = this.comboBox1.Text;
                        this.批号.Text = searchResult.Rows[i]["batchNo"].ToString();
                        this.型号.Text = searchResult.Rows[i]["productName"].ToString();
                        this.粒径.Text = searchResult.Rows[i]["productMark"].ToString();
                        this.重量.Text = searchResult.Rows[i]["weight"].ToString();
                        break;
                    }
                }
            }
        }

        private void 箱标打印机_SelectedIndexChanged(object sender, EventArgs e)
        {
            RegistryTool.WriteReg(regkeypath, "箱标打印机", this.箱标打印机.Text);
        }
    }
}
